package com.hui.server.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hui.server.config.redis.RedisCacheConfig;
import com.hui.server.entity.Department;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *              需要用到存储过程
 * @author hui
 * @since 2022-03-31
 */

public interface DepartmentMapper extends BaseMapper<Department> {


    /**
     * 查询所有部门，并按层级关系排列，循环递归查询
     * @param parentId
     * @return
     */
    @Select("select  \n" +
            "`id`, `name`, `parentId`, `depPath`, `enabled`, `isParent`\n" +
            "from \n" +
            "t_department\n" +
            "WHERE\n" +
            "parentId =#{parentId}")
    @Results(value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "children", many = @Many(select = "getAllDepartment"), column = "id")
    })
    List<Department> getAllDepartment(@Param("parentId") Integer parentId);


    /**
     * 调用添加部门的存储过程<p/>
     * 大坑：jdbcType=VARCHAR 全部要按照规定的大小写书写，否则项目跑不起来<p/>
     * 不能使用缓存，设置 useCache = false
     * @param department
     */
    @Select("call my_add_dep(#{name,mode=IN,jdbcType=VARCHAR}," +
            "#{parentId,mode=IN,jdbcType=INTEGER}," +
            "#{enabled,mode=IN,jdbcType=BOOLEAN}," +
            "#{result,mode=OUT,jdbcType=INTEGER}," +
            "#{id,mode=OUT,jdbcType=INTEGER}"+
            ");")
    @Options(statementType = StatementType.CALLABLE,useCache = false)
    void addDep(Department department);


    /**
     * 删除部门，调用存储过程
     * @param department
     */
    @Select("call my_delete_dep(" +
            "#{id,mode=IN,jdbcType=INTEGER}," +
            "#{result,mode=OUT,jdbcType=INTEGER}"+
            ");")
    @Options(statementType = StatementType.CALLABLE,useCache = false)
    void deleteDep(Department department);

}
